<?php
class Backend_Modules_Controller extends Backend_Controller{
    /**
     * (non-PHPdoc)
     * @see Backend_Controller::indexAction()
     */
     public function indexAction(){    
         $res = Resource::getInstance();    
	     $res->addJs('/js/app/system/crud/modules.js'  , 1);
	     $this->_resource->addInlineJs('
            var canPublish =  '.((integer)$this->_user->canPublish($this->_module)).';
        	var canEdit = '.((integer)$this->_user->canEdit($this->_module)).';
        	var canDelete = '.((integer)$this->_user->canDelete($this->_module)).';
        ');
    }
    
    
    public function listAction(){
        $modules = Config::factory(Config::File_Array , $this->_configMain->get('backend_modules'));
        
        $data = array();
        
        foreach ($modules as $k=>$v)
            $data[] = array_merge($v , array('name'=>$k));

        Response::jsonSuccess($data);    
    }
    
    public function updateAction(){
         if(!User_Admin::getInstance()->canEdit($this->_module))
            Response::jsonError($this->_lang->CANT_MODIFY);
        
         $data = Request::post('data', 'raw', false);
         
         if($data === false)
             Response::jsonError($this->_lang->INVALID_VALUE);
             
 
         $data = json_decode($data , true);
         if(!isset($data[0]))
         	$data = array($data);
         
         $modules = Config::factory(Config::File_Array , $this->_configMain->get('backend_modules'));
         foreach ($data as $v){
             $name = $v['name'];
             unset($v['name']);
             $modules->set($name, $v);
         }   
         
         if($modules->save())
             Response::jsonSuccess();
         else 
             Response::jsonError($this->_lang->CANT_WRITE_FS);    
    }
}